package file;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

public class FileManager 
{
	public static void writeToFile( Context fileContext, String filePath, String content )
	{
		writeToFile( fileContext, filePath, content.getBytes() );
	}
	
	public static void writeToFile( Context fileContext, String filePath, byte[] content ) 
	{
		try {
			FileOutputStream os = fileContext.getApplicationContext()
					.openFileOutput(filePath, Context.MODE_PRIVATE);
			os.write( content ); 
			os.close();
			System.out.println("Created file\n");
		} catch (IOException e) {
			System.out.print("Write Exception\n");
		}
	}
	
	public static String[] readLinesFromFile( String filePath )
	{
		List<String> lines = new ArrayList< String >();
		
		try {
		    BufferedReader br = new BufferedReader(new FileReader(filePath));
		    String line;

		    while ((line = br.readLine()) != null) 
		    {
		    	if( line.trim().length() == 0 )
		    		continue;
		    	lines.add( line.trim() );
		    }
		}
		catch (IOException e) {
		    //You'll need to add proper error handling here
		}
		return lines.toArray( new String[0] );
	}
	
	public static String readStringFromFile( String filePath )
	{
		//Read text from file
		StringBuilder text = new StringBuilder();

		try {
		    BufferedReader br = new BufferedReader(new FileReader(filePath));
		    String line;

		    while ((line = br.readLine()) != null) {
		        text.append(line);
		        text.append('\n');
		    }
		}
		catch (IOException e) {
		    //You'll need to add proper error handling here
		}
		return text.toString();
	}
}
